<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Task extends AdminController
{
    public $_validation = null;

    function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->load->library('FormFilter');
        $this->load->helper('formfilter');
        $this->load->model('MAdminGroup');
        $allGroups = $this->MAdminGroup->getAllGroupsByType(1);

        $this->assign('allGroups', $allGroups);
        $this->layout('task');
    }

    public function taskoperate(){
        $this->load->library('FormFilter');
        $this->load->helper('formfilter');
        $this->load->model('MAdminGroup');
        $allGroups = $this->MAdminGroup->getAllGroupsByType(1);

        $this->assign('allGroups', $allGroups);
        $this->layout();
    }

    public function ajaxTaskData()
    {
        $this->load->model('MAdminGroup');
        $this->load->model('MTask');
        $this->load->library('FormFilter');
        $this->load->helper('formfilter');
        $data = $this->input->post();
        $begintime = filterValue('task_create_time_begin');
        $endtime = filterValue('task_create_time_end');

        //销售组
        $allGroups = $this->MAdminGroup->getAllGroupsByType(1);

        if ($data['group_id']) {
            $allGroups = $this->MAdminGroup->getOne(['id' => $data['group_id']]);
            $allGroups = [$data['group_id'] => $allGroups->name];
        }
        $list = $this->MTask->getCountNum($begintime, $endtime, (int)$data['x_value'], $allGroups, $data['type']);
        if (!$list) {
            exit(json_encode([
                'success' => 0,
                'msg' => '筛选X下标有误'
            ]));
        }
        $legend = ['data' => array_values($allGroups)];

        $return = [
            'legend' => $legend,
            'xAxis' => $list['xAxis'],
            'series' => $list['series']
        ];
        exit(json_encode([
            'success' => 1,
            'msg' => '获取成功',
            'data' => $return,
        ]));
    }

    function ajaxTaskPieData()
    {
        $this->load->model('MAdminGroup');
        $this->load->model('MTask');
        $this->load->library('FormFilter');
        $this->load->helper('formfilter');
        $data = $this->input->post();
        $begintime = filterValue('task_create_time_begin');
        $endtime = filterValue('task_create_time_end');

        //销售组
        $allGroups = $this->MAdminGroup->getAllGroupsByType(1);
        $legend = ['data' => array_values($allGroups)];

        $list = $this->MTask->getPieData($begintime, $endtime,  $allGroups, $data['type']);

        $return = [
            'legend' => $legend,
            'series' => [
                'data' => array_values($list)
            ]
        ];
        exit(json_encode([
            'success' => 1,
            'msg' => '获取成功',
            'data' => $return,
        ]));
    }
}
